import java.util.Date;

public class MyLinkedList implements IList{

    /*
    构成MyLinkdeList 是一个一个节点
    该节点 有三个域
    val 数值域
    next 地址域
    prev 前驱域
     */

    //创建一个静态内部类
    static class ListNode{
        public int val;
        public ListNode prev;
        public ListNode next;
        //提供一个构造方法
        public ListNode(int val) {
            this.val = val;
        }
    }
    //成员
    public ListNode head;//当前链表的头结点
    public ListNode last;//当前链表的尾节点

    /*
    addFirst  头插法
     */
    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if (head == null){
            head = node;
            last = node;
        }else {
            node.next = head;
            head.prev = node;
            head = node;
        }
    }

    /*
    尾插法 addLast
     */
    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if (head == null){
            head = null;
            last = null;
        }else {
            last.next = node;
            node.prev = last;
            last = node;
        }
    }

    /*
    addIndex 往列表任意位置插
     */
    @Override
    public void addIndex(int index, int data) {
        ListNode node = new ListNode(data);
        if (head == null){
            head = null;
            last = null;
        }
        if (index < 0 && index > size()){
            throw new IndexException("双向链表插入，index不合法" + index);
        }
        if (index == 0){
            addFirst(data);
            return;
        }
        if (index == size()){
            addLast(data);
            return;
        }
        //中间插入
        if (index > 0 && index < size()){
            ListNode cur = findIndex(index);
            //先绑定后面
            node.next = cur;
            cur.prev.next = node;
            node.prev = cur.prev;
            cur.prev = node;
        }
    }
    /*
    这个方法是检测双向链表 走到cur
     */
    private ListNode findIndex(int index){
        ListNode cur = head;
        while (index != 0){
            cur = cur.next;
            index--;
        }
        return cur;
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = head;
        while (cur != null){
            if (cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    /*
    remove 删除指定的元素
     */
    @Override
    public void remove(int key) {
        ListNode cur = head;
        //一般情况 要删除的元素在中间
        //进行遍历
        while (cur != null){
            if (cur.val == key){
                //判断是不是头结点
                if (cur == head){
                    head = head.next;
                    if (head != null){
                        head.prev = null;
                    }else {
                        last = null;
                    }
                }else {
                    cur.prev.next = cur.next;
                    //判断尾节点
                    if (cur.next != null){
                        cur.next.prev = cur.prev;
                        //cur.prev.next = cur.next;
                    }else {
                        //cur.prev.next = cur.next;
                        last = last.prev;
                    }
                }
                return;
            }
            cur = cur.next;
        }
    }

    @Override
    public void removeAllKey(int key) {
        ListNode cur = head;
        //一般情况 要删除的元素在中间
        //进行遍历
        while (cur != null){
            if (cur.val == key){
                //判断是不是头结点
                if (cur == head){
                    head = head.next;
                    if (head != null){
                        head.prev = null;
                    }else {
                        last = null;
                    }
                }else {
                    cur.prev.next = cur.next;
                    //判断尾节点
                    if (cur.next != null){
                        cur.next.prev = cur.prev;
                        //cur.prev.next = cur.next;
                    }else {
                        //cur.prev.next = cur.next;
                        last = last.prev;
                    }
                }
            }
            cur = cur.next;
        }
    }

    /*
    size  判断链表的长度
     */
    @Override
    public int size() {
        //定义一个计数器
        int count = 0;
        ListNode cur = head;
        while (cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    @Override
    public void clear() {
        //第一种
        this.head = null;
        this.last = null;
        //第二种
        /*ListNode cur = head;
        while (cur != null){
            ListNode curNext = cur.next;
            cur.next = null;
            cur.prev = null;
            cur = curNext;
        }
        this.head = null;
        this.last = null;*/
    }

    /*
    display 用来打印MyLinkedList双向链表
    跟单向链表的打印一样
    从前向后走
     */
    @Override
    public void display() {
        //定义一个临时变量cur
        ListNode cur = head;
        //走完整个链表  判断条件 cur != null
        while (cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
}
